package appcus

import (
	"counter-help/admin/server/model/app"
	appReq "counter-help/admin/server/model/app/request"
	"counter-help/admin/server/model/common/response"
	"fmt"
	"github.com/gin-gonic/gin"
)

type CustomizationApi struct{}

// 创建新的定制请求
func (api *CustomizationApi) CreateCustomization(c *gin.Context) {
	var req appReq.CreateCustomization
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage("数据绑定失败", c)
		return
	}
	newCustomization := app.Customization{
		AnalysisRequest: req.AnalysisRequest,
		ContactName:     req.ContactName,
		Message:         req.Message,
		PhoneNumber:     req.PhoneNumber,
		UrgencyLevel:    req.UrgencyLevel,
	}
	if err := customizationService.CreateCustomization(newCustomization); err != nil {
		fmt.Println("当前错误提交", err)
		response.FailWithMessage("创建定制请求失败", c)
		return
	}

	response.OkWithMessage("创建定制请求成功", c)
}

// 根据处理状态获取定制请求
func (api *CustomizationApi) GetCustomizationsByStatus(c *gin.Context) {
	var request struct {
		IsHandled bool `json:"isHandled"`
	}
	if err := c.ShouldBindJSON(&request); err != nil {
		response.FailWithMessage("请求参数错误", c)
		return
	}

	customizations, err := customizationService.GetAllCustomizationsByStatus(request.IsHandled)
	if err != nil {
		response.CommonFailWithMessage("获取定制请求失败", c)
		return
	}

	response.OkWithDetailed(customizations, "获取定制请求成功", c)
}

// 标记请求为已处理
func (api *CustomizationApi) MarkAsHandled(c *gin.Context) {
	var request struct {
		IsHandled bool `json:"isHandled"`
		ID        uint `json:"id"`
	}
	if err := c.ShouldBindJSON(&request); err != nil {
		response.FailWithMessage("请求参数错误", c)
		return
	}

	if err := customizationService.MarkAsHandled(request.ID, request.IsHandled); err != nil {
		response.CommonFailWithMessage("标记处理失败", c)
		return
	}

	response.OkWithMessage("标记为已处理成功", c)
}
